using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using SamasDBI;
using SamasDBI.MySQL;
using EQEmuMysql;

namespace EQEmulatorGUI
{
    public partial class frmRules : Form
    {
        private SortedList<string, Rules> lsRules;
        private Rules objRule;
        private bool flgRulesLoaded = false;
        public string ruleset = System.Configuration.ConfigurationManager.AppSettings["ruleset"];
        public frmRules()
        {
            InitializeComponent();
        }
        public void RulesList()
        {
            flgRulesLoaded = false;
            ListViewItem lsvItem;
            lsvRules.Items.Clear();

            if (flgRulesLoaded == false)
            {
                lsRules = Lists.RuleList();
                flgRulesLoaded = false;
            }
            foreach (Rules oRule in lsRules.Values)
            {
                lsvItem = new ListViewItem(oRule.RuleName);
                lsvItem.Tag = oRule.RuleName;

                lsvRules.Items.Add(lsvItem);
            }
        }

        private void frmRules_Load(object sender, EventArgs e)
        {
            RulesList();
        }

        private void lsvRules_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (lsvRules.SelectedItems.Count <= 0)
            {
                return;
            }

            objRule = lsRules[lsvRules.SelectedItems[0].Tag.ToString()];

            txtRuleName.Text = objRule.RuleName;
            txtValue.Text = objRule.Value;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            objRule.RuleName = txtRuleName.Text;
            objRule.Value = txtValue.Text;

            if (objRule.Save() == true)
            {
                MessageBox.Show("Update Successfull.", "Update Rule");
                ClearRule();
                RulesList();
            }
            else
                MessageBox.Show("Update Failed.", "Update Rule");
        }
        private void ClearRule()
        {
            txtRuleName.Text = "";
            txtValue.Text = "";
        }
    }
}